﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using Eis51.CampanyCMS.Business.Bus;

using Eis51.CampanyCMS.FrameWork;
using Eis51.CampanyCMS.Model.Bus;


using Eis51.CampanyCMS.Business;
using Eis51.CampanyCMS.Model;
using Eis51.CampanyCMS.Model.Bus.QueryO;
using Eis51.Web.Admin;

namespace Eis51.CampanyCMS.UI.Admin.ProductsManager
{
    /// <summary>
    /// ProductsList 的摘要说明。
    /// </summary>
    public partial class PurchaseList : BasePage, IBindCondtion
    {


        protected void Page_Load(object sender, System.EventArgs e)
        {
            // 在此处放置用户代码以初始化页面
            if (!IsPostBack)
            {
                InitPage();
            }
        }

        private void InitPage()
        {

            PurchaseBLL.BindDropListTreeType(drList, AppContext.GetCurrentName(), AppContext.LCID);
            drList.Items.Insert(0, new ListItem("全部", ""));

            if (Request["strCon"] != null)
            {
                BingCondition(Request["strCon"]);
            }


            btnSearch_Click(null, null);
        }


        protected void btnSearch_Click(object sender, System.EventArgs e)
        {
            string s = "http://" +
                       System.Web.HttpContext.Current.Request.Url.Host + ":" +
                       System.Web.HttpContext.Current.Request.Url.Port + "/" +
                       System.Web.HttpContext.Current.Request.ApplicationPath +
                       ConfigurationSettings.AppSettings["MoreUpLoadFile"];

            string scondition = " title like '%{0}%'  and Licd={1}  ";

            //string sTreeList = "";

            scondition = string.Format(scondition, txtName.Text.Trim(), AppContext.LCID);

            if (drList.SelectedValue != "")
            {
                //sTreeList = ProductsTypeBLL.GetInstance().GetSubTreeType(drList.SelectedValue);
                scondition = scondition + " and ProductID = {0} ";
                scondition = string.Format(scondition, drList.SelectedValue);
            }
            if (AppContext.CurrentAdministrator != null)
            {
                scondition = scondition + " and  Createby='{0}' ";
                scondition = string.Format(scondition, AppContext.CurrentAdministrator.UserName);
            }

            scondition += " order by [Level] desc ";

            IList it = PurchaseBLL.GetInstance().Search(scondition);
            this.dgNews.Data = it;
            dgNews.DataBind();

        }

        protected void dgNews_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
        {

            if (e.Item.ItemType == ListItemType.AlternatingItem ||
                e.Item.ItemType == ListItemType.Item)
            {
                LinkButton btnDelete = e.Item.FindControl("btnDelete") as LinkButton;
                LinkButton btnModify = e.Item.FindControl("btnModify") as LinkButton;
                if (btnDelete != null)
                {
                    btnDelete.Attributes.Add("onclick", "javascript:return confirm('确定要删除该购买信息吗?');");
                }
                Products model = ProductsBLL.GetInstance().Get(new Products.Key(int.Parse(e.Item.Cells[3].Text)));
                if (null != model)
                {
                    e.Item.Cells[3].Text = model.Title;
                }
                LinkButton btnIsFst = e.Item.FindControl("btnIsFst") as LinkButton;
                if (e.Item.Cells[1].Text == "0")
                {
                    btnIsFst.Text = "否";
                }
                else
                {
                    btnIsFst.Text = "是";
                }

            }
        }

        protected void dgNews_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {

            switch (e.CommandName)
            {
                case "Delete":

                    PurchaseBLL.GetInstance().Delete(e.Item.Cells[0].Text);
                    this.btnSearch_Click(null, null);
                    break;
                case "Modify":
                    string url = "NewPurchase.aspx?seqno={0}";
                    url = string.Format(url, e.Item.Cells[0].Text);
                    string r = "";
                    r = UrlUtility.AddOrReplaceParam("PurchaseList.aspx", "strCon", GetConditionString(), true);
                    url = UrlUtility.AddOrReplaceParam(url, "r", r, true);
                    Response.Redirect(url);
                    break;
                case "FPage":
                    string IsFirst = e.Item.Cells[1].Text == "0" ? "1" : "0";
                    string sql = " update Purchase set IsFirst={0}  where  Seqno={1}";
                    sql = string.Format(sql, IsFirst, e.Item.Cells[0].Text);
                    PurchaseBLL.GetInstance().Execute(sql);
                    btnSearch_Click(null, null);
                    break;
                default:

                    break;
            }
        }

        protected void btnAdd_Click(object sender, System.EventArgs e)
        {
            string url = "NewPurchase.aspx";
            string r = "";
            r = UrlUtility.AddOrReplaceParam("PurchaseList.aspx", "strCon", GetConditionString(), true);
            url = UrlUtility.AddOrReplaceParam(url, "r", r, true);

            Response.Redirect(url);
        }

        #region IBindCondtion 成员
        class Condition : BasePageCondionRemeberHandler
        {

            public Condition()
                : base("")
            {

            }
            public Condition(string condion)
                : base(condion)
            {

            }
            private string mProductType;
            public string ProductType
            {
                get { return mProductType; }
                set { mProductType = value; }
            }
            private string txtName;
            public string TxtName
            {
                get { return txtName; }
                set { txtName = value; }
            }

            public string BeginDate { get; set; }
            public string EndDate { get; set; }

            public string PageSize { get; set; }
            public string CurrentPageIndex { get; set; }

        }
        public string GetConditionString()
        {
            Condition c = new Condition();
            c.ProductType = this.drList.SelectedValue;
            c.TxtName = txtName.Text;
            c.BeginDate = txtBeginDate.Text;
            c.EndDate = txtEndDate.Text;
            c.PageSize = this.dgNews.PageSize.ToString();
            c.CurrentPageIndex = this.dgNews.CurrentPageIndex.ToString();
            return c.ToString();
        }

        public void BingCondition(string conditionString)
        {
            Condition c = new Condition(conditionString);
            if (c.ProductType != "")
            {
                this.drList.SelectedValue = c.ProductType;
            }
            txtName.Text = c.TxtName;
            txtBeginDate.Text = c.BeginDate;
            txtEndDate.Text = c.EndDate;
            this.dgNews.PageSize = int.Parse(c.PageSize);
            this.dgNews.CurrentPageIndex = int.Parse(c.CurrentPageIndex);

        }

        #endregion
    }
}

